package lab2;

import static org.junit.Assert.*;
import java.util.NoSuchElementException;
import lab2.ClosedHashSet.CHSIterator;
import org.junit.Test;

public class CHSTest {

	@Test
	public void test_iteratorHasNext() {
		ClosedHashSet myClosedHashSet1=new ClosedHashSet(5);
		myClosedHashSet1.add(1);
		CHSIterator iterator=myClosedHashSet1.iterator();
		assertTrue(iterator.hasNext());
		iterator.next();
		assertFalse(iterator.hasNext());
	}

	@Test
	public void test_iteratorNextException() {
		ClosedHashSet myClosedHashSet1=new ClosedHashSet(5);
		CHSIterator iterator=myClosedHashSet1.iterator();
		try {
			iterator.next();
			fail("Exrpted NoSuchElementException");
		} catch(NoSuchElementException e) {
			assertTrue(e.getMessage().equals("There's no elements anymore!"));
		}
	}
	
	@Test
	public void test_addFull() {
		ClosedHashSet myClosedHashSet1=new ClosedHashSet(2);
		try {
			myClosedHashSet1.add(1);
			myClosedHashSet1.add(1);
			myClosedHashSet1.add(1);
			fail("Exrpted ArrayIndexOutOfBoundsException");
		} catch(ArrayIndexOutOfBoundsException e)
		{
			assertNull(e.getMessage());
		}
	}
	
	@Test
	public void test_addNull() {
		ClosedHashSet myClosedHashSet1=new ClosedHashSet(2);
		try {
			myClosedHashSet1.add(null);
			fail("Exrpted IllegalArgumentException");
		} catch(IllegalArgumentException e)
		{
			assertTrue(e.getMessage().equals("Argument can't be null"));
		}
	}	

	@Test
	public void test_iteratorNext() {
		ClosedHashSet myClosedHashSet1 = new ClosedHashSet(5);
		myClosedHashSet1.add(1);
		myClosedHashSet1.add(2);
		CHSIterator iterator=myClosedHashSet1.iterator();
		assertTrue(iterator.next() == 1);
		assertTrue(iterator.next() == 2);			
	}
	
	@Test
	public void test_size() {
		ClosedHashSet myClosedHashSet1 = new ClosedHashSet(5);
		assertTrue(myClosedHashSet1.size() == 0);	
		myClosedHashSet1.add(1);
		assertTrue(myClosedHashSet1.size() == 1);	
		myClosedHashSet1.add(2);
		assertTrue(myClosedHashSet1.size() == 2);		
	}

	@Test
	public void test_add() {
		ClosedHashSet myClosedHashSet1 = new ClosedHashSet(5);
		assertFalse(myClosedHashSet1.contains(1));
		assertTrue(myClosedHashSet1.add(1));
		assertTrue(myClosedHashSet1.contains(1));
		assertTrue(myClosedHashSet1.add(2));
		assertTrue(myClosedHashSet1.contains(2));			
	}
	
	@Test
	public void test_contains() {
		ClosedHashSet myClosedHashSet1 = new ClosedHashSet(5);
		assertFalse(myClosedHashSet1.contains(1));
		myClosedHashSet1.add(1);
		assertTrue(myClosedHashSet1.contains(1));
		myClosedHashSet1.remove(1);
		myClosedHashSet1.add(2);
		assertTrue(myClosedHashSet1.contains(2));	
		assertFalse(myClosedHashSet1.contains(1));
		assertTrue(myClosedHashSet1.contains(2));			
	}	

	@Test
	public void test_remove() {
		ClosedHashSet myClosedHashSet1 = new ClosedHashSet(15);
		myClosedHashSet1.add(1);
		myClosedHashSet1.remove(1);
		assertFalse(myClosedHashSet1.contains(1));
		myClosedHashSet1.add(-2);
		assertFalse(myClosedHashSet1.contains(1));
		assertTrue(myClosedHashSet1.contains(-2));		
		myClosedHashSet1.add(1);
		myClosedHashSet1.add(-2);
		myClosedHashSet1.add(2);
		myClosedHashSet1.remove(1);
		assertFalse(myClosedHashSet1.contains(1));
		assertTrue(myClosedHashSet1.contains(-2));		
		assertTrue(myClosedHashSet1.contains(2));		
		myClosedHashSet1.add(1);
		myClosedHashSet1.add(-2);
		myClosedHashSet1.add(2);
		myClosedHashSet1.add(5);
		myClosedHashSet1.remove(1);
		assertFalse(myClosedHashSet1.contains(1));
		assertTrue(myClosedHashSet1.contains(-2));		
		assertTrue(myClosedHashSet1.contains(2));		
		assertTrue(myClosedHashSet1.contains(5));			
	}

	@Test
	public void test_isEmpty() {
		ClosedHashSet myClosedHashSet1 = new ClosedHashSet(5);
		assertTrue(myClosedHashSet1.isEmpty());
		myClosedHashSet1.add(1);
		assertFalse(myClosedHashSet1.isEmpty());
		myClosedHashSet1.add(2);
		assertFalse(myClosedHashSet1.isEmpty());		
	}

	@Test
	public void test_clear() {
		ClosedHashSet myClosedHashSet1 = new ClosedHashSet(5);
		myClosedHashSet1.add(1);
		myClosedHashSet1.add(2);
		assertFalse(myClosedHashSet1.isEmpty());
		myClosedHashSet1.clear();
		assertTrue(myClosedHashSet1.isEmpty());
	}

	@Test
	public void test_toString() {
		ClosedHashSet myClosedHashSet1 = new ClosedHashSet(5);
		assertEquals(myClosedHashSet1.toString(),"[]");
		myClosedHashSet1.add(2);
		assertEquals(myClosedHashSet1.toString(),"[2]");
		myClosedHashSet1.add(1);
		assertEquals(myClosedHashSet1.toString(),"[1, 2]");
		myClosedHashSet1.add(3);
		assertEquals(myClosedHashSet1.toString(),"[1, 2, 3]");
		myClosedHashSet1.clear();
		assertEquals(myClosedHashSet1.toString(),"[]");
	}


}
